//
//  CTChangePWD.m
//  CT
//
//  Created by liufanwei on 15/4/7.
//  Copyright (c) 2015年 liufanwei. All rights reserved.
//

#import "CTChangePWD.h"
#import "BDKNotifyHUD.h"

@implementation CTChangePWD
@synthesize delegate;


-(void)awakeFromNib{
    [super awakeFromNib];
    _SmallBG.layer.cornerRadius = 4;
    _SmallBG.layer.masksToBounds = YES;
    _SmallBG.layer.borderWidth = 1;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:Nil];
}

- (IBAction)onCancelClick:(id)sender {
    [self removeFromSuperview];
}

- (IBAction)onCommitClick:(id)sender {
    NSString * oldpwd = _OldNumber.text;
    NSString * newpwd = _NewPWD.text;
    NSString * rnewpwd = _RNewPWD.text;
    if (![rnewpwd isEqualToString:newpwd] || rnewpwd.length == 0 || !rnewpwd) {
        [BDKNotifyHUD notifyHUDWithText:@"两次密码不一致"];
        return;
    }
    
    if (!oldpwd || oldpwd.length == 0) {
        [BDKNotifyHUD notifyHUDWithText:@"请输入老密码"];
        return;
    }
    
    if (delegate) {
        [delegate onSureClicked:@{@"old":oldpwd,@"new":newpwd}];
    }
}

-(void)hidKeyBoard{
    [_NewPWD resignFirstResponder];
    [_OldNumber resignFirstResponder];
    [_RNewPWD resignFirstResponder];
}



- (void)keyboardWillShow:(NSNotification *)notif {
        if (self.hidden == YES) {
            return;
        }
    
        CGRect rect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGFloat y = rect.origin.y;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.25];
        NSArray *subviews = [self subviews];
        for (UIView *sub in subviews) {
    
            CGFloat maxY = CGRectGetMaxY(sub.frame);
            if (maxY > y - 2) {
                sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, sub.center.y - maxY + y - 2);
            }
        }
        [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notif {
    if (self.hidden == YES) {
        return;
    }
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];
    NSArray *subviews = [self subviews];
    for (UIView *sub in subviews) {
        if (sub.center.y < CGRectGetHeight(self.frame)/2.0) {
            sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
        }
    }
    [UIView commitAnimations];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
